package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import de.dnb.basics.applicationComponents.DbProperties;

public class CreateDB {

	public static void main(String[] args) {

		Connection con = null;

		try {
			DbProperties properties = new DbProperties(
					"./dbconfig/db.properties");
			con = DriverManager.getConnection(properties.getUrl(),
					properties.getUserName(), properties.getPassword());

			Statement stmt = con.createStatement();

			stmt.executeUpdate("SET IGNORECASE TRUE");
			stmt.executeUpdate("DROP TABLE IF EXISTS sw CASCADE");
			stmt.executeUpdate("DROP TABLE IF EXISTS rel CASCADE");
			stmt.executeUpdate("CREATE CACHED TABLE "
					+ "PUBLIC.sw(id VARCHAR(12) PRIMARY KEY,"
					+ "type CHAR, level INTEGER, name VARCHAR(550)) ");
			stmt.executeUpdate("CREATE INDEX IDX_SW_NAME ON "
					+ "PUBLIC.sw(name)");
			stmt.executeUpdate("CREATE INDEX IDX_SW_Type_NAME ON "
					+ "PUBLIC.sw(type, name)");
			stmt.executeUpdate("CREATE CACHED TABLE PUBLIC.rel"
					+ "(ub VARCHAR(12) NOT NULL, " + "ob VARCHAR(12) NOT NULL"
					+ ", code VARCHAR(4))");
			stmt.executeUpdate("CREATE INDEX IDX_REL_UB ON PUBLIC.rel(ub)");
			stmt.executeUpdate("CREATE INDEX IDX_REL_OB ON PUBLIC.rel(ob)");
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}
}
